package com.casic.modules.sys.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.casic.common.base.service.impl.BaseServiceImpl;

import com.casic.modules.sys.dao.SysUserDeptDao;
import com.casic.modules.sys.entity.SysUserDeptEntity;
import com.casic.modules.sys.service.SysUserDeptService;

import java.util.*;


@Service("sysUserDeptService")
public class SysUserDeptServiceImpl extends BaseServiceImpl<SysUserDeptDao,SysUserDeptEntity>  implements SysUserDeptService {
    @Autowired
    public void setDao(SysUserDeptDao sysUserDeptDao) {
        this.dao=sysUserDeptDao;
    }

    /** 生成主键策略 */
    public String createId() {
        return UUID.randomUUID().toString().replaceAll("\\-", "");
    }

    @Override
    public void saveOrUpdate(String userId, List<String> deptIdList) {
        if(deptIdList.size() == 0){
            return ;
        }
        if(userId!=null){
            //先删除用户与角色关系
            this.dao.delete(userId);
        }
        List<SysUserDeptEntity> dataList=new ArrayList<>();
        SysUserDeptEntity sysUserDeptEntity=null;
        for(String deptId:deptIdList){
            sysUserDeptEntity=new SysUserDeptEntity();
            sysUserDeptEntity.setId(createId());
            sysUserDeptEntity.setDeptId(deptId);
            sysUserDeptEntity.setUserId(userId);
            dataList.add(sysUserDeptEntity);
        }
        //保存用户与角色关系
        Map<String, Object> map = new HashMap<>();
        map.put("deptIdList", dataList);
        this.dao.save(map);
    }
    @Override
    public List<String> queryDeptIdList(String userId) {
        return this.dao.queryDeptIdList(userId);
    }

    @Override
    public void delete(String userId) {
        this.dao.delete(userId);
    }

    @Override
    public void deleteBatch(String[] userId) {
        this.dao.deleteBatch(userId);
    }
    @Override
    public void deleteBatchByDeptId(String[] deptIds) {
        this.dao.deleteBatchByDeptId(deptIds);
    }
    @Override
    public List<String> findByDeptId(String deptId) {
        return this.dao.selectByDeptId(deptId);
    }
}
